package cn.thoughtworks.school.programCenter.services;

import cn.thoughtworks.school.programCenter.entities.AssignmentQuizTag;
import cn.thoughtworks.school.programCenter.entities.User;
import cn.thoughtworks.school.programCenter.entities.UserAssignmentQuizTag;
import cn.thoughtworks.school.programCenter.exceptions.BusinessException;
import cn.thoughtworks.school.programCenter.repositories.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AssignmentQuizTagService {
    @Autowired
    private AssignmentQuizTagRepository assignmentQuizTagRepository;
    @Autowired
    private UserAssignmentQuizTagRepository userAssignmentQuizTagRepository;
    @Autowired
    private UserCenterService userCenterService;
    @Autowired
    AssignmentRepository assignmentRepository;

    @Transactional
    public void editAssignmentQuizTags(Long assignmentId, Long quizId, Long studentId, Long userId, List<Long> tagIds) {
        List<UserAssignmentQuizTag> userAssignmentQuizTags;
        if(isBasicQuiz(assignmentId)){
            userAssignmentQuizTags = tagIds.stream()
                    .map(item -> new UserAssignmentQuizTag(studentId, new Date(), userId, assignmentId, null, item))
                    .collect(Collectors.toList());
            userAssignmentQuizTagRepository.deleteByOperatorIdAndStudentIdAndAssignmentId(userId, studentId, assignmentId);
        }else {
            userAssignmentQuizTags = tagIds.stream()
                    .map(item -> new UserAssignmentQuizTag(studentId, new Date(), userId, assignmentId, quizId, item))
                    .collect(Collectors.toList());
            userAssignmentQuizTagRepository.deleteByOperatorIdAndStudentIdAndAssignmentIdAndQuizId(userId, studentId, assignmentId, quizId);
        }
        userAssignmentQuizTagRepository.saveAll(userAssignmentQuizTags);
    }

    @Transactional
    public void deleteAssignmentQuizTags(Long assignmentQuizTagId) {
        assignmentQuizTagRepository.deleteById(assignmentQuizTagId);
        userAssignmentQuizTagRepository.deleteByAssignmentQuizTagId(assignmentQuizTagId);
    }

    public AssignmentQuizTag addAssignmentQuizTag(User current, AssignmentQuizTag assignmentQuizTag) {
        assignmentQuizTag.setCreatorId(current.getId());
        assignmentQuizTag.setCreateTime(new Date());
        return assignmentQuizTagRepository.save(assignmentQuizTag);
    }

    public List<UserAssignmentQuizTag> getStudentAssignmentQuizTags(Long assignmentId, Long quizId, Long studentId, User current) {
        if (isBasicQuiz(assignmentId)) {
            return userAssignmentQuizTagRepository.findByAssignmentIdAndStudentIdAndOperatorId(assignmentId, studentId, current.getId());
        }
        return userAssignmentQuizTagRepository.findByAssignmentIdAndQuizIdAndStudentIdAndOperatorId(assignmentId, quizId, studentId, current.getId());
    }

    @Transactional
    public void editAssignmentQuizTag(Long assignmentQuizTagId, AssignmentQuizTag assignmentQuizTag) throws BusinessException {
        AssignmentQuizTag assignmentQuizTagInfo = assignmentQuizTagRepository.findById(assignmentQuizTagId).orElseThrow(() -> new BusinessException("assignmentQuizTag 不存在"));
        assignmentQuizTagInfo.setColor(assignmentQuizTag.getColor());
        assignmentQuizTagInfo.setName(assignmentQuizTag.getName());

        assignmentQuizTagRepository.save(assignmentQuizTagInfo);

    }

    @Transactional
    public List<Map> getAssignmentQuizTagsByProgramId(Long programId) {
        ObjectMapper oMapper = new ObjectMapper();
        List<AssignmentQuizTag> assignmentQuizTags = assignmentQuizTagRepository.findByProgramId(programId);
        List<String> idList = assignmentQuizTags.stream().map(item -> item.getCreatorId().toString()).collect(Collectors.toList());
        List<Map> users = userCenterService.getUsersByIds(String.join(",", idList));
        return assignmentQuizTags.stream().map(item -> {
            Optional userInfo = users.stream()
                    .filter(user -> Long.valueOf(user.get("id").toString()).equals(item.getCreatorId()))
                    .findFirst();
            Map map = oMapper.convertValue(item, Map.class);
            if (userInfo.isPresent()) {
                Map user = oMapper.convertValue(userInfo.get(), Map.class);
                map.put("creator", user.get("username"));
            }
            return map;
        }).collect(Collectors.toList());
    }

    private boolean isBasicQuiz(Long assignmentId) {
        return "BASIC_QUIZ".equals(assignmentRepository.findById(assignmentId).get().getType());
    }
}